//
//  NewestViewController.swift
//  FM
//
//  Created by x on 2017/10/12.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class NewestViewController: UIViewController {

    /// 保存当前加载的数据的下标
    var index:Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        setupUI()
    }

    func setupUI() -> Void {
        view.backgroundColor = UIColor.white
        
        //1.添加表格
        view.addSubview(newestTableView)
        
        //2.加载社区内容
        loadNewestData(offset: 0)
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        newestTableView.frame = self.view.bounds
    }
    
    /// 上拉刷新加载更多数据方法
    func loadMoreEsseceData() -> Void {
        index += 1
        let num  = index * 10
        loadNewestData(offset: num)
    }
    
    /// 加载社区数据方法
    ///
    /// - Parameter offset: <#offset description#>
    func loadNewestData(offset:Int) -> Void {
        Essence.loadEssence(type: 0, offset: offset) { (data, error) in
            if error == nil {
                self.newestList += data!
                self.newestTableView.reloadData()
            }else {
                print("loadNewestData======\(error!)")
            }
        }
    }
    
    //MARK: -- 懒加载
    
    private lazy var newestTableView:UITableView = {
        let tableView = UITableView()
        tableView.frame = self.view.bounds
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        
        //注册cell
        tableView.register(EssenceCell.self, forCellReuseIdentifier: NewestCellIdentifier)
        
        //添加上拉刷新
        tableView.mj_footer = MJRefreshBackNormalFooter(refreshingTarget: self, refreshingAction: #selector(loadMoreEsseceData))
        
        return tableView

    }()
    
    /// 表格显示数据源
    fileprivate lazy var newestList:[Essence] = [Essence]()
    
    /// 缓存cell行高
    fileprivate lazy var cachesHeight:[String:CGFloat] = [String:CGFloat]()

}

private let NewestCellIdentifier = "NewestCellIdentifier"

extension NewestViewController:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return newestList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: NewestCellIdentifier) as! EssenceCell
        cell.model = newestList[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = newestList[indexPath.row]
        let essenceCommentVC = EssenceCommentViewController(model: model)
        essenceCommentVC.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(essenceCommentVC, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        //1.首先重缓存中取cell的高度
        
        let model = newestList[indexPath.row]
        let height = cachesHeight[model.id!]
        
        //如果缓存中有直接返回
        guard height == nil else {
            return height!
        }
        
        //2.如果缓存中没有
        let cell = tableView.dequeueReusableCell(withIdentifier: NewestCellIdentifier) as! EssenceCell
        
        let rowHeight = cell.rowHeight(essence: model)
        
        //3.把得到的高度存到内存中
        cachesHeight[model.id!] = rowHeight
        
        return rowHeight
    }

}








